<template>
  <el-row :gutter="30">

    <el-col :span="7">
      <el-link disabled>应用列表</el-link>
      <el-table :data="apps" style="width: 100%" :show-header="false" @row-click="loadRight" class="pointer">
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>
      </el-table>

    </el-col>

    <el-col :span="7">
      <el-link disabled>角色列表</el-link>

      <el-table :data="roles" style="width: 100%" :show-header="false" @row-click="getRoleResource" class="pointer">
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="delRole(scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>

      <el-dialog title="添加角色" :visible.sync="roleVisible">
        <el-form :model="roleModel">
          <el-form-item label="所属应用">
            <el-input v-model="roleModel.appId" autocomplete="off" readonly></el-input>
          </el-form-item>
          <el-form-item label="角色名称">
            <el-input v-model="roleModel.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleModel.msg" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="roleVisible = false;roleModel={}">取 消</el-button>
          <el-button type="primary" @click="addRole">确 定</el-button>
        </div>
      </el-dialog>
      <el-button type="primary" size="mini" v-if="selectApp.id" round
                 @click="roleModel.appId = selectApp.id;roleVisible=true">添加角色
      </el-button>
    </el-col>


    <el-col :span="7">
      <el-link disabled>资源树 ({{selectApp.name?selectApp.name:'未选择'}}<-->{{selectRole.name?selectRole.name:'未选择'}})
      </el-link>

      <el-tree
        :data="resourcesTree"
        :props="resourcesTreeProps"
        node-key="id"
        :default-expand-all="true"
        :check-strictly="true"
        ref="tree"
        show-checkbox>
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <el-tooltip class="item" effect="dark" :content="data.id+'--'+data.parentId+'--'+data.name+'--'+data.code+'--'+data.locator"
                      placement="right">
              <span>
                <i class="el-icon-s-tools" v-if="data.actionFlag">--></i>{{ data.name }}
              </span>
           </el-tooltip>
        </span>
      </el-tree>

      <el-button type="primary" size="mini" round @click="updateRoleResource">更新角色资源</el-button>
    </el-col>


  </el-row>


</template>
<script>


  export default {
    name: "Role",
    mounted() {
      this.getApps();
    },
    data() {
      return {
        apps: [],
        roles: [],
        selectApp: {},
        selectRole: {},
        roleVisible: false,
        roleModel: {},
        resourcesTreeProps: {
          label: 'name',
          children: 'ssoResourceTreeViews'
        },
        resourcesTree: [],
      }
    },
    methods: {
      getApps() {
        this.$http.get("/back/apps").then((data) => {
          this.apps = data;
        }).catch(error => {
          console.error(error)
        });
      },

      loadRight(app) {
        this.selectApp = app;
        this.selectRole = {};
        this.getAppResources(app)
        this.getAppRole(app)
      },
      getAppRole(app) {
        this.$http.get("/back/roles?appId=" + app.id).then((data) => {
          this.roles = data;
        }).catch(error => {
          console.error(error)
        });
      }
      ,
      getAppResources(app) {
        this.$http.get("/back/resources?appId=" + app.id).then((data) => {
          this.resourcesTree = data;
        }).catch(error => {
          console.error(error)
        });
      },

      getRoleResource(role) {
        this.selectRole = role;
        this.$http.get("/back/roleResources?appId=" + role.applicationId + "&roleId=" + role.id).then((data) => {
          this.$refs.tree.setCheckedKeys(data);
        }).catch(error => {
          console.error(error)
        });
      },
      addRole() {
        this.$http.post("/back/role", this.roleModel).then((data) => {
          this.roles = data;
          this.roleModel = {};
          this.roleVisible = false;
          this.$message.info("增加角色成功！")
        }).catch(error => {
          this.$message.error("增加角色异常！")
        });
      }
      ,
      updateRoleResource() {
        if (this.selectApp.id == this.selectRole.applicationId) {
          let roleResources = new Set();
          this.$refs.tree.getCheckedNodes().forEach(data => {
            roleResources.add(data.id);
          })
          this.$http.post("/back/roleResource", {
            appId: this.selectRole.applicationId,
            roleId: this.selectRole.id,
            resourceIds: roleResources
          }).then((data) => {
            this.$refs.tree.setCheckedKeys(data);
            this.$message.info("角色与资源关系修改成功")
          }).catch(error => {
            this.$message.error("修改角色与资源关系异常！")
          });
        } else {
          this.$message.error("应用或者角色选择错误！")
        }

      },
      delRole(role) {
        this.$http.delete("/back/delRole?roleId=" + role.id + "&appId=" + role.applicationId).then((data) => {
          this.roles = data;
        }).catch(error => {
          this.$message.error("删除角色异常！")
        });
      }
    },
  }
</script>
<style>
  .pointer {
    cursor: pointer;
  }

</style>
